//
//  QianDaoController.m
//  ToListenPhone
//
//  Created by zhibao on 2023/7/20.
//

#import "FLSQianDaoController.h"
#import "FLSHaoPingDetailController.h"
#import "FLSFenXiangDetailController.h"
#import "FLSJifenListController.h"
#import "FLSShopController.h"
@interface FLSQianDaoController (){
    UIScrollView *scrollview;
    UIButton *jifenBtn;
    UIButton *qiandaoBtn;
    
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)int lianxuNum;
@property(nonatomic,assign)int qiandaoState;
@end

@implementation FLSQianDaoController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [jifenBtn setTitle:[NSString stringWithFormat:@"%@ >",[UserInfoManager sharedManager].integral] forState:UIControlStateNormal];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_backImage = [UIImage imageNamed:@"arrow_back"];
    self.gk_statusBarStyle = UIStatusBarStyleDarkContent;
    [self hideNavLine];
    _lianxuNum = 0;
    _qiandaoState = 0;
    
    UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
    [appearance configureWithOpaqueBackground];
    appearance.backgroundColor = UIColor.clearColor;
    //去掉导航栏下的阴影线
    appearance.shadowColor = UIColor.clearColor;
    self.gk_navigationBar.standardAppearance = appearance;
    self.gk_navigationBar.scrollEdgeAppearance = self.gk_navigationBar.standardAppearance;
    
    [Helper setDiferentColors:@[(id)MyColor(@"#FFDFC3").CGColor,(id)MyColor(@"#FFE9D1").CGColor] WithView:self.view withFlag:ColorDirectionLeft];
    
    scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MY_Screen_W, MY_Screen_H)];
    [self.view addSubview:scrollview];
    [self getData];
}

-(void)getData{
    __weak __typeof__(self) weakSelf = self;
    [[HttpHelper sharedInstance] sendGet:@"api/sign/in/history" parameters:@{} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        [self->scrollview removeAllSubviews];
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        strongSelf.lianxuNum = [responseObject.data[@"data"][@"count"] intValue];
        strongSelf.dataArray = responseObject.data[@"data"][@"weeks"];
        strongSelf.qiandaoState = [responseObject.data[@"data"][@"state"] intValue];
        [strongSelf initView];
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}

-(void)initView{
    UIImageView *logoImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fulishe_qiandao_top"]];
    [scrollview addSubview:logoImgView];
    [logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(pixw((253)));
    }];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(15, 104 - 64 + NAVIH, 105, 90)];
    topView.backgroundColor = MyColor(@"#FFFBF5");
    topView.layer.cornerRadius = 15;
    [scrollview addSubview:topView];
    
    UIImageView *cjifenImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fulishe_qiandao_jifen"]];
    [topView addSubview:cjifenImgView];
    [cjifenImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
    }];
    
    jifenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [jifenBtn setTitle:[NSString stringWithFormat:@"%@ >",[UserInfoManager sharedManager].integral] forState:UIControlStateNormal];
    [jifenBtn setTitleColor:MyColor(@"#E26038") forState:UIControlStateNormal];
    jifenBtn.tag = 1000;
    [jifenBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    jifenBtn.titleLabel.font = MY_SCALE_SemiboldFONT(13);
    [topView addSubview:jifenBtn];
    [jifenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cjifenImgView);
        make.left.equalTo(cjifenImgView.mas_right).offset(5);
    }];
    
    UIImageView *cduihuanImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fulishe_qiandao_duihuan"]];
    [topView addSubview:cduihuanImgView];
    [cduihuanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(cjifenImgView.mas_bottom).offset(23);
    }];
    
    UIButton *duihuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [duihuanBtn setTitle:@"去兑换 >" forState:UIControlStateNormal];
    [duihuanBtn setTitleColor:MyColor(@"#E26038") forState:UIControlStateNormal];
    duihuanBtn.titleLabel.font = MY_SCALE_SemiboldFONT(13);
    [duihuanBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    duihuanBtn.tag = 1001;
    [topView addSubview:duihuanBtn];
    [duihuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cduihuanImgView);
        make.left.equalTo(cjifenImgView.mas_right).offset(5);
    }];
    
    UIView *qiandaoView = [[UIView alloc]initWithFrame:CGRectMake(15, topView.bottom + 45, MY_Screen_W - 30, 205)];
    qiandaoView.layer.cornerRadius = 15;
    qiandaoView.backgroundColor = UIColor.whiteColor;
    [scrollview addSubview:qiandaoView];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = MY_SCALE_RegularFONT(15);
    [qiandaoView addSubview:titleLabel];
    [Helper setLabelInfo:@[@[@"已连续签到",[NSString stringWithFormat:@"%d",self.lianxuNum],@"天"],@[MYBlock,MyColor(@"#E26038"),MYBlock]] withLabel:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(15);
    }];
    
    
    CGFloat labelWidth = (qiandaoView.width - 30 - 6 * 20)/7;
    for (int i = 0 ; i < self.dataArray.count; i ++) {
        NSDictionary *dic = self.dataArray[i];
        UIButton *jifenBtn = [[UIButton alloc]initWithFrame:CGRectMake(15 + (labelWidth + 20) * i, 60, labelWidth, labelWidth)];
        jifenBtn.layer.cornerRadius = labelWidth / 2;
        [qiandaoView addSubview:jifenBtn];
        jifenBtn.backgroundColor = MyColor(@"#FFF7EA");
        [jifenBtn setTitleColor:MyColor(@"#FEA115") forState:UIControlStateNormal];
       
        if([dic[@"state"] intValue] == 1){
            [jifenBtn setImage:[UIImage imageNamed:@"fulishe_qiandao_yes_label"] forState:UIControlStateNormal];
        }else{
            [jifenBtn setTitle:[NSString stringWithFormat:@"+%@",dic[@"integral"]] forState:UIControlStateNormal];
        }
        jifenBtn.titleLabel.font = MY_SCALE_SemiboldFONT(10);
        
        UILabel *weekLabel = [[UILabel alloc]initWithFrame:CGRectMake(jifenBtn.left, jifenBtn.bottom + 7, jifenBtn.width + 20, labelWidth)];
        [qiandaoView addSubview:weekLabel];
        weekLabel.centerX = jifenBtn.centerX;
        weekLabel.textColor = MyColor(@"999999");
        int week = [dic[@"week"] intValue];
        if(week == 1){
            weekLabel.text = LocalizedString(@"星期一");
        } else if(week == 2){
            weekLabel.text = LocalizedString(@"星期二");
        } else if(week == 3){
            weekLabel.text = LocalizedString(@"星期三");
        } else if(week == 4){
            weekLabel.text = LocalizedString(@"星期四");
        } else if(week == 5){
            weekLabel.text = LocalizedString(@"星期五");
        } else if(week == 6){
            weekLabel.text = LocalizedString(@"星期六");
        } else if(week == 7){
            weekLabel.text = LocalizedString(@"星期日");
        }
        
        weekLabel.font = MY_SCALE_RegularFONT(11);
        weekLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    qiandaoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qiandaoBtn.frame = CGRectMake(82, 139, qiandaoView.width - 164, 46);
    qiandaoBtn.layer.cornerRadius = 23;
    if(self.qiandaoState == 0){
        qiandaoBtn.backgroundColor = MyColor(@"#FEA115");
        [qiandaoBtn setTitle:@"立即签到" forState:UIControlStateNormal];
    }else{
        [qiandaoBtn setTitle:@" 已签到 " forState:UIControlStateNormal];
        [qiandaoBtn setImage:[UIImage imageNamed:@"fulishe_qiandao_yes_btn"] forState:UIControlStateNormal];
        qiandaoBtn.backgroundColor = MyColor(@"#FFD28E");
    }
    [qiandaoBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    
    qiandaoBtn.titleLabel.font = MY_SCALE_SemiboldFONT(16);
    [qiandaoView addSubview:qiandaoBtn];
    __weak __typeof__(self) weakSelf = self;
    [qiandaoBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        if([[self->qiandaoBtn titleForState:UIControlStateNormal] isEqualToString:@"立即签到"]){
            DLog(@"立即签到");
            [[HttpHelper sharedInstance] sendPost:@"api/sign/in" parameters:@{} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
                if(responseObject.code == 1){
                    [QMUITips showSucceed:@"签到成功"];
                    [[UserInfoManager sharedManager] getUserInfoData:^(BOOL flag) {
                        [self->jifenBtn setTitle:[NSString stringWithFormat:@"%@ >",[UserInfoManager sharedManager].integral] forState:UIControlStateNormal];
                    }];
                    [strongSelf getData];
                }else{
                    [QMUITips showError:@"签到失败"];
                }
            } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
                
            }];
        }
        
    }]];
    
    UILabel *cruleLabel = [UILabel new];
    cruleLabel.text = @"签到规则";
    cruleLabel.textColor = MyColor(@"#BF9E7C");
    cruleLabel.font = MY_SCALE_SemiboldFONT(13);
    [scrollview addSubview:cruleLabel];
    [cruleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(qiandaoView.mas_bottom).offset(40);
    }];
    
    UILabel *cruleContentLabel = [UILabel new];
    cruleContentLabel.text = @"1、活动用户必须为APP注册用户；\n\n 2、用户每天进入签到中心后点击立即签到，获得当日的签到积分，积分可以用于兑换各种权益，点击“去兑换”可以去使用； \n\n3、签到从周一到周日的7个自然日为签到周期，连续签到的记录将在下一个周一开始重新计算； \n\n4、签到中断后，累计签到的天数将重新开始计算，签到奖励将在当前奖励基础上继续计算。 \n\n5、所获的积分有效期为12个月，从积分获得之当月起算。12个月届满后的次月1日，12个月前当月所获得的全部积分将会自动清零。例如：2023年2月1日将会自动清零2022年2月之前所发放的所有积分。";
    cruleContentLabel.textColor = MyColor(@"#BF9E7C");
    cruleContentLabel.numberOfLines = 0;
    cruleContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cruleContentLabel.font = MY_SCALE_RegularFONT(12);
    [scrollview addSubview:cruleContentLabel];
    [cruleContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(cruleLabel.mas_bottom).offset(15);
        make.bottom.equalTo(scrollview).offset(-30);
    }];
}

-(void)btnClick:(UIButton *)sender{
    if(sender.tag == 1000){
        DLog(@"1000积分");
        FLSJifenListController *controller = [FLSJifenListController new];
        [self.navigationController pushViewController:controller animated:YES];
    }else if (sender.tag == 1001){
        DLog(@"去兑换");
        FLSShopController *controller = [FLSShopController new];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

@end
